package com.songshan.service;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.songshan.aspect.LogAdvice;
import com.songshan.bean.Role;
import com.songshan.bean.RoleMenu;
import com.songshan.bean.User;
import com.songshan.dao.RoleMapper;
import com.songshan.dao.RoleMenuMapper;
import com.songshan.dao.UserRoleMapper;
import com.songshan.dto.PageParameter;
import com.songshan.exception.BusinessException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RoleManageService {

  @Autowired
  private RoleMapper roleMapper;

  @Autowired
  private RoleMenuMapper roleMenuMapper;

  @Autowired
  private UserRoleMapper userRoleMapper;

  public Map<String, Object> queryRoleWithPage(PageParameter pageParameter) {
    Map<String, Object> result = new HashMap<>();
    Page page = null;
    try {
      page = PageHelper.startPage(pageParameter.getPageNumber(), pageParameter.getPageSize());
      List<Role> list = roleMapper.selectRoleWithPage(pageParameter.getParams());
      result.put("total", page.getTotal());
      result.put("rows", list);
    } finally {
      if (page != null) {
        page.close();
      }
    }
    return result;
  }

  @LogAdvice
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void relateMenu(Map<String, Object> param) throws BusinessException {
    Integer roleId = (Integer) param.get("roleId");
    if (roleId == null) {
      throw new BusinessException(500,"请选择角色！");
    }
    roleMenuMapper.deleteRoleMenu(roleId);
    List<Integer> menuIds = (List<Integer>) param.get("menuIds");
    if (menuIds == null || menuIds.isEmpty()) {
      return;
    }
    Date date = new Date();
    for (Integer menuId : menuIds) {
      RoleMenu roleMenu = new RoleMenu();
      roleMenu.setRoleId(roleId);
      roleMenu.setMenuId(menuId);
      roleMenu.setCreateTime(date);
      roleMenuMapper.insertRoleMenu(roleMenu);
    }
  }

  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void addRole(Role role) throws BusinessException {
    Role existsRole = roleMapper.selectRoleByCode(role.getCode());
    if (existsRole != null) {
      throw new BusinessException(500,role.getCode() + "已存在！");
    }
    role.setCreateTime(new Date());
    roleMapper.insertRole(role);
  }

  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void modRole(Role srcRole) throws BusinessException {
    Role role = roleMapper.selectRoleById(srcRole.getId());
    BeanUtil.copyProperties(srcRole, role);
    Role existsRole = roleMapper.selectRoleByCode(role.getCode());
    if (existsRole != null && !existsRole.getId().equals(role.getId())) {
      throw new BusinessException(500,role.getCode() + "已存在！");
    }
    role.setModifyTime(new Date());
    roleMapper.updateRole(role);
  }

  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void delRole(Integer id) throws BusinessException {
    roleMenuMapper.deleteRoleMenu(id);
    userRoleMapper.deleteUserRoleByRoleId(id);
    roleMapper.deleteRoleById(id);
  }

  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public void roleStatus(Integer id,Integer status) throws BusinessException{
    Role role = roleMapper.selectRoleById(id);
    role.setStatus(status);
    role.setModifyTime(new Date());
    roleMapper.updateRole(role);
  }
}
